package org.zfes.snowier.common.monitor.biz.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.zfes.snowier.common.monitor.biz.dao.ClassLoadMapper;
import org.zfes.snowier.common.monitor.biz.model.ClassLoadEntity;
import org.zfes.snowier.common.monitor.util.ByteKbUtils;
import org.zfes.snowier.common.monitor.util.JvmUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;


@Service
public class ClassService {
    @Autowired
    private ClassLoadMapper classLoadMapper;
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ClassLoadEntity> findAllByInstanceKey(String instanceKey) {
    	Map<String, Object> paramMap=Maps.newHashMap();
    	paramMap.put("instanceKey", instanceKey);
        return 	classLoadMapper.selectList(new QueryWrapper(paramMap));
    }

    /**
     *  写入类加载信息
     * @param name
     * @param date
     * @param jstatClass
     */
    public void save(ClassLoadEntity entity) {
    	entity.setDate(JvmUtils.time());
    	classLoadMapper.insert(entity);
    }


    @Async
    public void clearAll() {
    	classLoadMapper.deleteAll();
    }
    @Async
   	public void save( String instanceKey,String clazzJsonData) {
       	 ClassLoadEntity entity =JSONObject.parseObject(clazzJsonData, ClassLoadEntity.class);
       	 entity.setBytes1(ByteKbUtils.getMB(entity.getBytes1()));
       	 entity.setBytes2(ByteKbUtils.getMB(entity.getBytes2()));
       	 
       	entity.setInstanceKey(instanceKey);
       	entity.setGroupKey(StringUtils.substringBefore(instanceKey, "_"));
    	 
       	 save( entity);
   		
   	}
    
}
